package com.juzipi.thread.test.demo;

import java.util.ArrayList;

/**
 * @Author juzipi
 * @Date 2021/6/16 9:06
 * @Info
 */
public class UnsafeList {

    public static void main(String[] args) {
        //ArrayList线程不安全
        ArrayList<String> objects = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                synchronized (objects) {
                    objects.add(Thread.currentThread().getName());
                }
            }).start();

        }
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(objects.size());
    }


}